<%@ page contentType="text/html; charset=UTF-8"%>
<%
	//String path = request.getContextPath();
	//String width = "180";
%>

<div id="relPrjDiv">

<div class="caption">
	<span class="title">关联项目信息</span>
</div>
<div class="fieldBox col_50">
	<div class="label">关联金融机构</div>
	<div class="field">
        <input id="fundId" name="fundId"  type="text" class="ui-textbox" value="" placeholder="请选择"  ltype="combobox" validate="">*
    </div>
</div>
<div class="fieldBox col_50">
	<div class="label">关联项目</div>
	<div class="field">
        <input id="fncPrjId" name="fncPrjId"  type="text" class="ui-textbox" value="" placeholder="请选择"  ltype="combobox" validate="">*
    </div>
</div>
<div class="fieldBox col_50">
	<div class="label">报价名称</div>
	<div class="field">
		<input id="fncScmNm" name="fncScmNm" type="text" disabled="disabled"
			class="ui-textbox"  validate=""
			value="${fncCapPayeeInfoBigVO.fncScmNm}" /> *
	</div>
</div>

<div class="fieldBox col_50">
	<div class="label">是否更新实际XIRR(%)</div>
	<div class="field">
		<select id="isXirr" name="isXirr"  validate="" onchange="hiddenOrShowXirrDiv();">
			<option value="">请选择</option>
			<option value="1" <c:if test="${fncCapPayeeInfoBigVO.isXirr =='1'}">selected="selected"</c:if>>是</option>
			<option value="0" <c:if test="${fncCapPayeeInfoBigVO.isXirr =='0'}">selected="selected"</c:if>>否</option>
		</select> *[本次付款成功后更新]
	</div>
</div>

<div id="xirrDiv">
<div class="fieldBox col_50">
	<div class="label">原实际XIRR(%)</div>
	<div class="field">
		<input id="xirrOld" name="xirrOld" type="text" disabled="disabled"
			class="ui-textbox"  validate=""
			value="${fncCapPayeeInfoBigVO.xirrOld}" /> *
	</div>
</div>
<div class="fieldBox col_50">
	<div class="label">新实际XIRR(%)</div>
	<div class="field">
		<input id="xirrNew" name="xirrNew" type="text"
			class="ui-textbox"  validate=""
			value="${fncCapPayeeInfoBigVO.xirrNew}" /> *
			<c:if test="${operate == 'add' ||operate == 'update'}">	
				<span id="countXirr" class="sq-button sq-button-round" onclick="countXirr();">计算</span>
			</c:if>
	</div>
</div>
</div>

<div class="caption">
	<span class="title">计费信息</span>
	<c:if test="${operate == 'add' ||operate == 'update'}">
		<div style="margin-left: auto;margin-right: 10px;width:200px;margin-top: -15px">
			<span  class="sq-button sq-button-round" onclick="addNewScmChrg();">新增
			</span>
			<span class="sq-button sq-button-round" onclick="deleteRow();" >删除选中行
			</span>
		</div>
	</c:if>
</div>
<div class="fieldBox col_100">
	<div id="scmChrg" class="sq-grid" ></div>
</div>

<div class="fieldBox col_100">
	<div class="label"></div>
	<div class="field"></div>
</div>

</div>

<script type="text/javascript">
	     
var mrgnUseMtdJSON = [];
<c:if test="${not empty mrgnUseMtdJSON}"> mrgnUseMtdJSON = ${mrgnUseMtdJSON}; </c:if> 
var feeListJSON = [];
<c:if test="${not empty feeListJSON}"> feeListJSON = ${feeListJSON}; </c:if>
var scmChrgGrid;
var fncPrjId;

//显示或隐藏关联项目信息/计费信息
function hiddenOrShowRelPrjDiv(){
	if($("#payType").val() == "3"){//付款类型:再融资首付款费用付款
		$("#relPrjDiv").show();
	}else{
		$("#relPrjDiv").hide();
	}
}

//显示或隐藏关联项目旧新实际XIRR(%)
function hiddenOrShowXirrDiv(){
	if($("#isXirr").val() == "1"){
		$("#xirrDiv").show();
	}else{
		$("#xirrDiv").hide();
	}
}

$(function() {
			
	
	//显示或隐藏关联项目信息/计费信息
	hiddenOrShowRelPrjDiv();
	hiddenOrShowXirrDiv();
	
	//关联金融机构
	 var ligerFundId = $("#fundId").ligerComboBox({
         valueField: "id",
         textField: "fundRasInsNm",
         autocomplete: function (e){ //模糊自动匹配
			filter_autoComboboxData(e.key, this);
			e.show();
		 },
		 onChangeValue:function(value){
			if(value){
				var fundId = liger.get("fundId").findValueByText(value);
				if(!fundId){
					return;
				}
				getFncPrjIdList(fundId);//获取关联项目
			}else{
				liger.get("fncPrjId").selectValue(" ");
			}
		}
	 });
	
	
	//关联项目
	var ligerFncPrjId =$("#fncPrjId").ligerComboBox({
         valueField: "id",
         textField: "fncprjnm",
		 onChangeValue:function(value){
			if(value){
				fncPrjId = liger.get("fncPrjId").findValueByText(value);
				<c:if test="${operate == 'add' ||operate == 'update'}">
					getFncPrjBscInfo(fncPrjId);//根据项目id,查询项目详情和报价
				</c:if>
			}
		}
	 });
	
	
	
	//初始化关联金融机构
	$.ajax({
		type: 'get',
		dataType:'json',
		url:'<%=path%>/rfbase/fncCapPayeeInfoBig/getFriBaseInfoVOList.do',
		success:function(msg){
			 if(msg){
				 	liger.get("fundId").setData(msg);
				 	liger.get("fundId").selectValue("${fncCapPayeeInfoBigVO.fundId}");
			 }
		}
	});
	
	//获取关联项目
	function getFncPrjIdList(fundId) {
		$.ajax({
			type: 'get',
			dataType:'json',
			url:'<%=path%>/rfbase/fncCapPayeeInfoBig/getFncPrjIdList.do?fundId='+fundId,
			success:function(msg){
				 if(msg){
					 	liger.get("fncPrjId").setData(msg);
					 	liger.get("fncPrjId").selectValue("${fncCapPayeeInfoBigVO.fncPrjId}");
				 }else{
					 liger.get("fncPrjId").clear();
					 liger.get("fncPrjId").setData({});//置空下拉框数据
				 }
			}
		});
	}
	
	//根据项目id,查询项目详情和报价
	function getFncPrjBscInfo(fncPrjId) {
		//清空报价名称 原实际XIRR(%)
		$("#fncScmNm").val("");
		$("#xirrOld").val("");
					 
		$.ajax({
			type: 'get',
			dataType:'json',
			url:'<%=path%>/rfbase/fncCapPayeeInfoBig/getFncPrjBscInfo.do?fncPrjId='+fncPrjId,
			success:function(msg){
				 if(msg){
					 $("#fncScmNm").val(msg.fncscmNM);
					 $("#xirrOld").val(msg.acXirr);
				 }
			}
		});
	}
	
	//初始化计费信息
	initScmChrgList();
	
	$("#ligerForm").ligerForm({
        validate : true
    });
	
	<c:if test="${empty operate || operate == 'view'}">
		// 详情页面---需要隐藏一些元素
		liger.get("fundId").setDisabled(true);//关联金融机构
		liger.get("fncPrjId").setDisabled(true);//关联项目
		liger.get("isXirr").setDisabled(true);
		liger.get("xirrNew").setDisabled(true);
	</c:if>
	
});
		
function initScmChrgList()
{ 
	//fncScmId=180317245000084;
	scmChrgGrid = $("#scmChrg").ligerGrid({
	    	url: '<%=path%>/rfbase/fncCapPayeeInfoBig/getFncScmChrgParmBigList.do?fncCapPayeeInfoBigId=${fncCapPayeeInfoBigVO.id}',
	    	method : 'get',
            columns: [
                { display: '费用名称', name: 'costNm', minWidth: 150 ,enabledSort:true,
            	       validate:{required:true,minlength:1},
            	editor: 
            	 {
                    type: 'select',
                    data: feeListJSON,
                    isMultiSelect: false,isShowCheckBox: false,
                    valueField: 'key',
                    textField: 'value'
		          },
	                render: function (item) {
	                	//console.log(item);
	                	return item.costNm;
	                }
                },
                { display: '是否保证金', width: 100, name: 'mrgnIs',type:'text',
	                  editor: { type: 'select', 
								data: [
				                    { value: '否', key: '0' },
				                    { value: '是', key: '1' }
				                ],
								valueField: 'key', textField: 'value' },
                    	render: function (item)
	                    {
	                        if (parseInt(item.mrgnIs) == 1) return '是';
	                        return '否';
	                    }
	                },
	                { display: '保证金到期处理方式', name: 'mrgnUseMtd', minWidth: 150 ,enabledSort:true,
	            	       validate:{required:true,minlength:1},
	            	editor: 
	            	 {
	                    type: 'select',
	                    data: mrgnUseMtdJSON,
	                    isMultiSelect: false,isShowCheckBox: false,
	                    valueField: 'key',
	                    textField: 'value'
			          },
		                valueField: 'mrgnUseMtd',
		                textField: 'mrgnUseMtdDes',
		                render: function (item) {
		                    return item.mrgnUseMtdDes;
		                }
	                },
               
                { display: '费用金额', name: 'costAmt', width: 150, type: 'float', editor: { type: 'float'} }, 
            { display: '是否计入首付款', name: 'costInTpCd',width: 150,
                    editor: { type: 'select', 
						data: [
		                    { value: '首付款', key: '1' },
		                ],
						valueField: 'key', textField: 'value' },
                render: function (item)
                {
                    return '首付款';
                }
            },
            { display: '首付款是否坐扣', name: 'wthrDdct',width: 150,
                editor: { type: 'select', 
							data: [
			                    { value: '否', key: '0' },
			                ],
							valueField: 'key', textField: 'value' },
                render: function (item)
                {
                    return '否';
                }
            },
            { display: '预计付款时间', name: 'prePayDate', type: 'date',render:renderDate,format: 'yyyy-MM-dd',editor: { type: 'date' } },
            
            ],
            onSelectRow: function (rowdata, rowindex)
            {
                $("#txtrowindex").val(rowindex);
            },
            <c:if test="${operate == 'add' || operate == 'update'}">
		        enabledEdit: true,
			</c:if>
            isScroll: false, 
            onBeforeEdit: f_onBeforeEdit,
            onBeforeSubmitEdit: f_onBeforeSubmitEdit,
            width: '100%',
	        root:'Rows',
	        record:'Total',
	        hideLoadButton : true,
	        width: '100%',
	        height:'100%',
	        heightDiff:-2,
	        checkbox:true,
	        usePager: false, 
	        rownumbers:true
        }); 	    
}

//是否计入首付款为‘是’的时候，“是否坐扣”可编辑 默认0
function f_onBeforeEdit(e){
    var manager = $("#scmChrg").ligerGetGridManager();
	var rowdata = manager.getRow(e.rowindex);
	var costin = rowdata.costInTpCd;
    if (e.column.name == "wthrDdct"){
    	if(costin == 1){
    		return true;
    	}else{
    		return false;
    	}
    }
}
function f_onBeforeSubmitEdit(e)
{ 
	var manager = $("#scmChrg").ligerGetGridManager();
	    var rowdata = manager.getRow(e.rowindex);
    var payval = e.value;
    if (e.column.name == "costPct")
    {          	
    	if($.isNumeric(Number(payval))){
    		var len = payval.substring(payval.indexOf(".")+1,payval.length).length;
    		var intval = payval;
    		if(payval.indexOf(".")>0){
    			intval = payval.substring(0,payval.indexOf("."));
    		}
    		if(len>6 || intval>99){
        		alert("数字不能大于100且小数位不能超过6位!");
        		return false;
    		}
    		//费用比例（%）费用金额只能填一项
    		if(payval !=null && payval !="" && payval !="0"&& rowdata.costAmt !=null && rowdata.costAmt !=""){
         	    //alert("计费信息中费用比例（%）和费用金额只能填写一项，请重新填写！");
 	           	//return false;
            }
    		return true;
    	}else{
    		alert("请输入数字!");
    		return false;
    	}     	
    }
    //费用比例（%）费用金额只能填一项
    if (e.column.name == "costAmt")
    {          	
     	 if( payval !=null && payval !="" && rowdata.costPct !=null && rowdata.costPct !=""&& rowdata.costPct !="0"){
     	   	// alert("计费信息中费用比例（%）和费用金额只能填写一项，请重新填写！");
	          // 	return false;
        }
    }
    return true;
}
function deleteRow(){ 
	scmChrgGrid.deleteSelectedRow();
}   
function addNewScmChrg(){
	//console.log(scmChrgGrid.recordNumber);
	if(scmChrgGrid.recordNumber == 0) 
		scmChrgGrid.addRow({});
} 

//格式化时间
function renderDate(rowdata, index, value){
	var dateStr = "";
	if(null != value){
		var date = new Date(value);
		dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
	}
	return dateStr;
}

//计算xirr
function countXirr() {
	if(!fncPrjId){
		$.ligerDialog.SQalert({content: "请选择关联项目",SQ_type: "warning"});
		return;
	} 
	var wait_dialog;
	$.ajax({
		type: 'post',
		dataType:'json',
		url:'<%=path%>/rfbase/fncCapPayeeInfoBig/getXIRR.do?prjid='+fncPrjId,
		beforeSend: function(){
		    wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
		},
		success:function(msg){
			if(msg && msg.success == 1){
				$("#xirrNew").val(msg.data);
			 }else{
				 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
			}
		},complete:function(msg){
			wait_dialog.close();
		}
	});
}


</script>
